package org.jeecg.modules.ParameterModule.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.ParameterModule.entity.*;
import org.jeecg.modules.ParameterModule.mapper.CategoryInfoMapper;
import org.jeecg.modules.ParameterModule.mapper.LocationMapper;
import org.jeecg.modules.ParameterModule.service.CategoryInfoService;
import org.jeecg.modules.ParameterModule.service.LocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LocationServiceImpl extends ServiceImpl<LocationMapper, LocationInfo> implements LocationService {
    @Autowired
    private LocationMapper locationMapper;

    /**
     * 分页查询仓库
     */
    @Override
    public IPage<WarehouseInfo> query_WarehousePageList(Page<WarehouseInfo> page, String queryName, String queryCode) {
        IPage<WarehouseInfo> pagelist = locationMapper.query_WarehousePageList(page,queryName,queryCode);
        return pagelist;
    }
    /**
     * 增加仓库
     */
    @Override
    public int add_warehouse(WarehouseInfo datainfo) {
        try {
            //先根据编号判断是否存在:0编号，1名称，2id
            WarehouseInfo warehouseInfo = locationMapper.query_warehouseBycode(datainfo.getWarehouseCode(),0);
            WarehouseInfo warehouseInfo2 = locationMapper.query_warehouseBycode(datainfo.getWarehouseName(),1);
            if(warehouseInfo2!=null){
                return 3;
            }else if(warehouseInfo!=null){
                return 2;
            } else {
                //添加
                locationMapper.add_warehouse(datainfo);
                return 0;
            }
        }catch (Exception e){
            System.out.println("添加失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 编辑仓库
     */
    @Override
    public int update_warehouse(WarehouseInfo datainfo) {
        int code = 1;
        try {
            //根据id查询仓库详情
            WarehouseInfo warehouseInfo = locationMapper.query_warehouseBycode(datainfo.getId(),2);
            if(warehouseInfo!=null){
                //名称改了编码没改
                if(!datainfo.getWarehouseName().equals(warehouseInfo.getWarehouseName()) && datainfo.getWarehouseCode().equals(warehouseInfo.getWarehouseCode())){//名称改了编码没改
                    WarehouseInfo warehouseInfo2 = locationMapper.query_warehouseBycode(datainfo.getWarehouseName(),1);
                    if(warehouseInfo2!=null){
                        code = 3;
                    }else {
                        code = 0;
                        locationMapper.update_warehouse(datainfo);
                    }
                }
                //名称没改编码改了
                else if(datainfo.getWarehouseName().equals(warehouseInfo.getWarehouseName()) && !datainfo.getWarehouseCode().equals(warehouseInfo.getWarehouseCode())){
                    WarehouseInfo warehouseInfo1 = locationMapper.query_warehouseBycode(datainfo.getWarehouseCode(),0);
                    if(warehouseInfo1!=null){
                        code = 2;
                    }else {
                        code = 0;
                        locationMapper.update_warehouse(datainfo);
                    }
                }
                //名称和编码同时改了
                else if(!datainfo.getWarehouseName().equals(warehouseInfo.getWarehouseName()) && !datainfo.getWarehouseCode().equals(warehouseInfo.getWarehouseCode())){
                    WarehouseInfo warehouseInfo2 = locationMapper.query_warehouseBycode(datainfo.getWarehouseName(),1);
                    WarehouseInfo warehouseInfo1 = locationMapper.query_warehouseBycode(datainfo.getWarehouseCode(),0);
                    if(warehouseInfo2!=null ){
                        code = 3;
                    }
                    if(warehouseInfo1!=null ){
                        code = 2;
                    }
                    if(warehouseInfo2==null && warehouseInfo1==null){
                        code = 0;
                        locationMapper.update_warehouse(datainfo);
                    }
                }else {//什么都没改动
                    code = 0;
                    locationMapper.update_warehouse(datainfo);
                }
            }
            return code;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }

    /**
     * 删除仓库
     */
    @Override
    public int delete_warehouse(String id) {
        try {
            locationMapper.delete_warehouse(id);
            return 0;
        }catch (Exception e){
            System.out.println("失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 根据id查询仓库
     */
    @Override
    public WarehouseInfo query_warehouseById(String id) {
        WarehouseInfo datainfo = locationMapper.query_warehouseBycode(id,2);
        return datainfo;
    }

    @Override
    public List<WarehouseInfo> query_allwarehouse() {
        List<WarehouseInfo> datalist = locationMapper.query_allwarehouse();
        return datalist;
    }

    /**
     * 分页查询库区
     */
    @Override
    public IPage<ReservoirInfo> query_ReservoirPageList(Page<ReservoirInfo> page, String topId,String queryName, String queryCode) {
        IPage<ReservoirInfo> pagelist = locationMapper.query_ReservoirPageList(page,topId,queryName,queryCode);
        return pagelist;
    }
    /**
     * 增加库区
     */
    @Override
    public int add_Reservoir(ReservoirInfo datainfo) {
        try {
            //先根据编号判断是否存在:0编号，1名称，2id
            ReservoirInfo warehouseInfo = locationMapper.query_ReservoirInfoById(datainfo.getWarehouseId(),datainfo.getReservoirCode(),0);
            ReservoirInfo warehouseInfo2 = locationMapper.query_ReservoirInfoById(datainfo.getWarehouseId(),datainfo.getReservoirName(),1);
            if(warehouseInfo2!=null){
                return 3;
            }else if(warehouseInfo!=null){
                return 2;
            } else {
                //添加
                locationMapper.add_Reservoir(datainfo);
                return 0;
            }
        }catch (Exception e){
            System.out.println("添加失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 编辑库区
     */
    @Override
    public int update_Reservoir(ReservoirInfo datainfo) {
        int code = 1;
        try {
            //根据id查询仓库详情
            ReservoirInfo warehouseInfo = locationMapper.query_ReservoirInfoById("",datainfo.getId(),2);
            if(warehouseInfo!=null){
                //只修改了名称，编号没改
                if(!datainfo.getReservoirName().equals(warehouseInfo.getReservoirName()) && datainfo.getReservoirCode().equals(warehouseInfo.getReservoirCode())){
                    ReservoirInfo warehouseInfo2 = locationMapper.query_ReservoirInfoById(warehouseInfo.getWarehouseId(),datainfo.getReservoirName(),1);
                    if(warehouseInfo2!=null){
                        code = 3;
                    }else {
                        code = 0;
                        locationMapper.update_Reservoir(datainfo);
                    }
                }
                //只修改了编号，名称没改
                else if(datainfo.getReservoirName().equals(warehouseInfo.getReservoirName()) && !datainfo.getReservoirCode().equals(warehouseInfo.getReservoirCode())){
                    ReservoirInfo warehouseInfo1 = locationMapper.query_ReservoirInfoById(warehouseInfo.getWarehouseId(),datainfo.getReservoirCode(),0);
                    if(warehouseInfo1!=null){
                        code = 2;
                    }else {
                        code = 0;
                        locationMapper.update_Reservoir(datainfo);
                    }
                }
                //二者同时修改
                else if(!datainfo.getReservoirName().equals(warehouseInfo.getReservoirName()) && !datainfo.getReservoirCode().equals(warehouseInfo.getReservoirCode())){
                    ReservoirInfo warehouseInfo2 = locationMapper.query_ReservoirInfoById(warehouseInfo.getWarehouseId(),datainfo.getReservoirName(),1);
                    if(warehouseInfo2!=null){
                        code = 3;
                    }
                    ReservoirInfo warehouseInfo1 = locationMapper.query_ReservoirInfoById(warehouseInfo.getWarehouseId(),datainfo.getReservoirCode(),0);
                    if(warehouseInfo1!=null){
                        code = 2;
                    }
                    if(warehouseInfo2==null && warehouseInfo1==null){
                        code = 0;
                        locationMapper.update_Reservoir(datainfo);
                    }
                }else {
                    code = 0;
                    locationMapper.update_Reservoir(datainfo);
                }
            }
            return code;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 修改库区
     */
    @Override
    public int delete_Reservoir(String id, String status) {
        try {
            locationMapper.delete_Reservoir(id,status);
            return 0;
        }catch (Exception e){
            System.out.println("失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 根据id查询库区
     */
    @Override
    public ReservoirInfo query_ReservoirInfoById(String id) {
        ReservoirInfo datainfo = locationMapper.query_ReservoirInfoById("",id,2);
        return datainfo;
    }

    /**
     * 分页查询货架
     */
    @Override
    public IPage<ShelvesInfo> query_shelvesPageList(Page<ShelvesInfo> page, String topId, String queryName, String queryCode) {
        IPage<ShelvesInfo> pagelist = locationMapper.query_shelvesPageList(page,topId,queryName,queryCode);
        return pagelist;
    }
    @Override
    public int add_shelves(ShelvesInfo datainfo) {
        try {
            //先根据编号判断是否存在:0编号，1名称，2id
            ShelvesInfo warehouseInfo = locationMapper.query_shelvesInfoById(datainfo.getReservoirId(),datainfo.getShelvesCode(),0);
            ShelvesInfo warehouseInfo2 = locationMapper.query_shelvesInfoById(datainfo.getReservoirId(),datainfo.getShelvesName(),1);
            if(warehouseInfo2!=null){
                return 3;
            }else if(warehouseInfo!=null){
                return 2;
            } else {
                //添加
                locationMapper.add_shelves(datainfo);
                return 0;
            }
        }catch (Exception e){
            System.out.println("添加失败=="+e.toString());
            return 1;
        }
    }
    @Override
    public int update_shelves(ShelvesInfo datainfo) {
        int code = 1;
        try {
            //根据id查询货架详情
            ShelvesInfo warehouseInfo = locationMapper.query_shelvesInfoById("",datainfo.getId(),2);
            if(warehouseInfo!=null){
                //只修改了名称，编号没改
                if(!datainfo.getShelvesName().equals(warehouseInfo.getShelvesName()) && datainfo.getShelvesCode().equals(warehouseInfo.getShelvesCode())){
                    ShelvesInfo warehouseInfo2 = locationMapper.query_shelvesInfoById(warehouseInfo.getReservoirId(),datainfo.getShelvesName(),1);
                    if(warehouseInfo2!=null){
                        code = 3;
                    }else {
                        locationMapper.update_shelves(datainfo);
                        code = 0;
                    }
                }
                //只修改了编号，名称没改
                else if(datainfo.getShelvesName().equals(warehouseInfo.getShelvesName()) && !datainfo.getShelvesCode().equals(warehouseInfo.getShelvesCode())){
                    ShelvesInfo warehouseInfo1 = locationMapper.query_shelvesInfoById(warehouseInfo.getReservoirId(),datainfo.getShelvesCode(),0);
                    if(warehouseInfo1!=null){
                        code = 2;
                    }else {
                        locationMapper.update_shelves(datainfo);
                        code = 0;
                    }
                }
                //二者同时修改
                else if(!datainfo.getShelvesName().equals(warehouseInfo.getShelvesName()) && !datainfo.getShelvesCode().equals(warehouseInfo.getShelvesCode())){
                    ShelvesInfo warehouseInfo2 = locationMapper.query_shelvesInfoById(warehouseInfo.getReservoirId(),datainfo.getShelvesName(),1);
                    ShelvesInfo warehouseInfo1 = locationMapper.query_shelvesInfoById(warehouseInfo.getReservoirId(),datainfo.getShelvesCode(),0);
                    if(warehouseInfo2!=null){
                        code = 3;
                    }
                    if(warehouseInfo1!=null){
                        code = 2;
                    }
                    if(warehouseInfo2==null && warehouseInfo1==null){
                        locationMapper.update_shelves(datainfo);
                        code = 0;
                    }
                }else {
                    locationMapper.update_shelves(datainfo);
                    code = 0;
                }
            }
            return code;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }
    @Override
    public int delete_shelves(String id, String status) {
        try {
            locationMapper.delete_shelves(id,status);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }
    @Override
    public ShelvesInfo query_shelvesInfoById(String id) {
        ShelvesInfo datainfo = locationMapper.query_shelvesInfoById("",id,2);
        return datainfo;
    }

    /**
     * 分页查询库位
     */
    @Override
    public IPage<LocationInfo> query_LocationPageList(Page<LocationInfo> page, String topId, String queryName, String queryCode) {
        IPage<LocationInfo> pagelist = locationMapper.query_LocationPageList(page,topId,queryName,queryCode);
        return pagelist;
    }
    /**
     * 增加库位
     */
    @Override
    public int add_Location(LocationInfo datainfo) {
        try {
            //先根据编号判断是否存在:0编号，1名称，2id
            LocationInfo warehouseInfo = locationMapper.query_LocationInfoById(datainfo.getShelvesId(),datainfo.getLocationCode(),0);
            LocationInfo warehouseInfo2 = locationMapper.query_LocationInfoById(datainfo.getShelvesId(),datainfo.getLocationName(),1);
            if(warehouseInfo2!=null){
                return 3;
            }else if(warehouseInfo!=null){
                return 2;
            } else {
                //添加
                locationMapper.add_Location(datainfo);
                return 0;
            }
        }catch (Exception e){
            System.out.println("添加失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 编辑库位
     */
    @Override
    public int update_Location(LocationInfo datainfo) {
        try {
            locationMapper.update_Location(datainfo);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 删除/启用库位
     */
    @Override
    public int delete_Location(String id, String status) {
        try {
            locationMapper.delete_Location(id,status);
            return 0;
        }catch (Exception e){
            System.out.println("修改失败=="+e.toString());
            return 1;
        }
    }
    /**
     * 根据id查询库位
     */
    @Override
    public LocationInfo query_LocationInfoById(String id) {
        LocationInfo datainfo = locationMapper.query_LocationInfoById("",id,2);
        return datainfo;
    }
    /**
     * 根据货架id统计下面所有的库位
     */
    @Override
    public int count_shelves_location(String top_id) {
        return locationMapper.count_shelves_location(top_id);
    }


}
